﻿using System;
using System.Collections.Generic;
using System.Linq;
using dIRca.Data.Settings;

namespace dIRca.Data.Services
{
    public class SettingService : ISettingService, IDisposable
    {
        #region Properties
        private DatabaseService _storageService;

        private readonly object _sync = new object();
        private bool _isLoaded;

        private Client _settings;
        public Client Settings
        {
            get
            {
                if (!_isLoaded)
                    LoadFromStorage();

                return _settings;
            }
        }
        #endregion

        public SettingService(DatabaseService storageService)
        {
            _storageService = storageService;
            if (!_storageService.IsRunning)
            {
                _storageService.Start();
                _storageService.Starting();
            }
        }

        private void LoadFromStorage()
        {
            if (_isLoaded)
                return;

            lock (_sync)
            {
                if (_isLoaded)
                    return;

                // HACK: Exceptions here usually mean underlying dataset changes. Perform a truncate?
                Client record = null;
                try
                {
                    record = _storageService.Database.Load<Client>(true);
                }
                catch (Exception exception)
                {
                    //throw;
                }
                //record = _storageService.Database.Query<Client, int>().FirstOrDefault();
                if (record == null)
                {
                    //_logger.Write("SettingService: No setting exists in IsolatedStorage, adding default");
                    _settings = Factory.Create();
                }
                else
                {
                    //_logger.Write("SettingService: Loading Property, hash code - " + client.GetHashCode());
                    //if (record.LazyValue.IsValueCreated)
                    //    _settings = record.LazyValue.Value;
                    _settings = record;
                }

                _isLoaded = true;
            }
        }

        public void Save(Client property)
        {
            if (property == null)
                return;
            if (!_isLoaded)
                LoadFromStorage();

            _storageService.Database.Save<Client>(property);
            _storageService.Database.Flush();
        }

        public void RestoreDefault()
        {
            _settings = Factory.Create();
            _storageService.Database.Save<Client>(_settings);
            _storageService.Database.Flush();
        }

        public void Dispose()
        {
            if (_storageService != null)
            {
                _storageService.Dispose();
                _storageService = null;
            }
        }
    }
}
